package com.zhouxiaoge.compiler;

import org.mdkt.compiler.InMemoryJavaCompiler;

import java.lang.reflect.Method;

public class Main {
    public static void main(String[] args) throws Exception {
        String sourceCode = """
                package com.zhouxiaoge.compiler.bo;
                
                import com.zhouxiaoge.compiler.User;
                
                /**
                 * @author 周小哥 gqzmy@163.com 2022/8/25 9:46
                 */
                
                public class Hello {
                    public String hello() {
                        User user = new User();
                        user.setName("zzz");
                        user.setAge(0);
                        return user.toString();
                    }
                }
                """;
        Class<?> helloClass = InMemoryJavaCompiler.newInstance().compile("com.zhouxiaoge.compiler.bo.Hello", sourceCode);
        Method hello = helloClass.getMethod("hello");
        Object invoke = hello.invoke(helloClass.getDeclaredConstructor().newInstance());
        System.out.println(invoke);

        String sourceCode1 = """
                package com.zhouxiaoge.compiler.bo;
                
                import com.zhouxiaoge.compiler.User;
                
                /**
                 * @author 周小哥 gqzmy@163.com 2022/8/25 9:46
                 */
                
                public class Hello {
                    public String hello() {
                        User user = new User();
                        user.setName("xxx");
                        user.setAge(0);
                        return user.toString();
                    }
                }
                """;
        Class<?> helloClass1 = InMemoryJavaCompiler.newInstance().compile("com.zhouxiaoge.compiler.bo.Hello", sourceCode1);
        Method hello1 = helloClass1.getMethod("hello");
        Object invoke1 = hello1.invoke(helloClass1.getDeclaredConstructor().newInstance());
        System.out.println(invoke1);

        Object invoke2 = hello.invoke(helloClass.getDeclaredConstructor().newInstance());
        System.out.println(invoke2);
    }
}
